/**
 * Copyright 2009 Takahiko Ito
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.unigram.oluolu.rqe;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.ToolRunner;

import org.unigram.oluolu.common.OluoluConstants;
import org.unigram.oluolu.rqe.postprocessor.SpellCheckPostProcessor;


/**
 * A child class of RelatedQueryDictionaryCreation. This class create
 * spelling correction dictionary from query log data.
 */
public final class SpellCheckDictionaryCreation
    extends RelatedQueryDictionaryCreation {

    /**
     * Add configurations to create spelling correction dictionary.
     *
     * @param conf adding specific configurations
     * @param args containing setup information such as 
     *         Levenshtein threshold.
     */
    protected void setSpecificConfiguration(
            final Configuration conf, final String[] args) {
        assert(args != null);
        
        /* adding default post processor */         
        conf.set(RqeConstants.RQE_POST_PROCESSOR,
        "org.unigram.oluolu.rqe.postprocessor.SpellCheckPostProcessor");
        
        /* additional settings from arguments */ 
        for (int i = 0; i < args.length; ++i) {
            if ("-levenshteinThreshold".equals(args[i])) {
                conf.setLong(SpellCheckPostProcessor.RQE_LEVENSHTEIN_THRESHOLD, 
                        Long.parseLong(args[++i]));
            } else if ("-likelihoodThredhold".equals(args[i])) {
                conf.setFloat(SpellCheckPostProcessor.LIKELIHOOD_THRESHOLD,
                        Float.parseFloat(args[++i]));
            } else if ("-lengthDiffThreshold".equals(args[i])) {
                conf.setFloat(SpellCheckPostProcessor.RQE_LENGTH_DIFF_THRESHOLD,
                        Float.parseFloat(args[++i]));
            } else if ("-inputLanguage".equals(args[i])) {
                conf.set(OluoluConstants.LANGUAGE, args[++i]);
            }
        }
    }
    
    /**
     * Show usage.
     */
    @Override
    protected void showParameters() {
        super.showCommonParamters();
        System.out.print("\n");
        System.out.println("Paramters for spelling check dictionary creation:");
        System.out.println("    [-likelihoodThredhold VALUE] "
                + "use VALUE as the threshold"
                + " of log likelihood ratio");
        System.out.println("    [-lengthThredhold VALUE]     "
                + "use VALUE as the threshold of "
                + "length difference threshold");
        System.out.println("    [-inputLanguage VALUE]       "
                + "use VALUE as the language of input data.");

    }

    /**
     * Main method.
     *
     * @param args arguments
     * @throws Exception -
     */
    public static void main(final String[] args) throws Exception {
        int exitCode = ToolRunner.run(
                new SpellCheckDictionaryCreation(), args);
        System.exit(exitCode);
    }
}
